//
//  DownloadViewDataModel.swift
//  YoruNeko
//
//
//  Created by liphone on 2021/6/10.
//


/**
 传入DownloadView 的数据模型基本单元，作为可观测数据，包含当前进度值，下载状态，下载url, 文件名字
 */
import Foundation
import UIKit


struct DownloadViewItem:Identifiable{
    
    var id:ObjectIdentifier
    let Obj = NSObject.init()
    
    var urlStr:String;
    var title:String;
    var time:Date?
    //var icon:UIImage = UIImage(systemName:"network")! //默认图像可以集中配置
    
    var progress:Float{
        Float(downloadedSize)/Float(totalSize)
    }
    
    var downloadedSize: Int64 = 0
    var totalSize:Int64
    var speedDescription:String=""
    var errorMessage:String?
    
    var state:State = .finished
    
    
    
    enum State{
        case finished,going,paused,stopped //stopped为因某种原因无法继续
    }
    
    init(_ title:String,urlStr:String,contentLength:Int64){
        self.title=title;
        self.urlStr=urlStr;
        self.totalSize=contentLength
        self.id = ObjectIdentifier.init(Obj)
    }
    
    func getStateDescriptionByState()->String{
        var str:String="";
        switch state {
            case .finished:
                str="已完成"
            case .going:
                str=String(format:"%s 下载中..",speedDescription)
            case .paused:
                str="已暂停"
            case .stopped:
                if let errMsg = errorMessage {
                    str=errMsg;
                }
                else {
                    str="已暂停"
                }
        }
        return str
    }
    
    func getEstimateProgress()->Float{
        return Float(downloadedSize/totalSize)
    }
    
    
    /**
     通过该setter方法，同步更新progressView中的进度值
    线程安全问题： 修改视图一般只在主线程中执行
    耦合度 较高，但目前无其他解决方法
     */
    func setDownloadedSize(downloadedSize:Int64){
        
    }
    
    func setBinderProgressView(){
        
    }
    


}

